package com.example.micro_property_springboot.controller;

import com.example.micro_property_springboot.pojo.AssetInfo;
import com.example.micro_property_springboot.pojo.VO.Result;
import com.example.micro_property_springboot.service.StatisticsService;
import com.example.micro_property_springboot.service.AssetCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 统计分析模块Controller
 * 提供资产统计分析相关的HTTP接口
 */
@RestController
@RequestMapping("/api/statistics")
@Api(tags = "统计分析模块")
public class StatisticsController {
    private static final Logger log = LoggerFactory.getLogger(StatisticsController.class);

    @Autowired
    private StatisticsService statisticsService;
    
    @Autowired
    private AssetCheckService assetCheckService;

    /**
     * 获取资产概览统计数据
     * @return 资产概览数据
     */
    @GetMapping("/overview")
    @ApiOperation("获取资产概览")
    public Result<Object> getAssetOverview() {
        Object overview = statisticsService.getAssetOverview();
        return Result.success(overview);
    }

    /**
     * 获取资产状态分布统计
     * @return 状态分布列表
     */
    @GetMapping("/status-distribution")
    @ApiOperation("获取资产状态分布")
    public Result<List<Object>> getAssetStatusDistribution() {
        List<Object> distribution = statisticsService.getAssetStatusDistribution();
        return Result.success(distribution);
    }

    /**
     * 获取部门资产分布统计
     * @return 部门分布列表
     */
    @GetMapping("/dept-distribution")
    @ApiOperation("获取部门资产分布")
    public Result<List<Object>> getAssetDeptDistribution() {
        List<Object> distribution = statisticsService.getAssetDeptDistribution();
        return Result.success(distribution);
    }

    /**
     * 获取资产分类分布统计
     * @return 分类分布列表
     */
    @GetMapping("/category-distribution")
    @ApiOperation("获取资产分类分布")
    public Result<List<Object>> getAssetCategoryDistribution() {
        List<Object> distribution = statisticsService.getAssetCategoryDistribution();
        return Result.success(distribution);
    }

    /**
     * 获取资产价值趋势数据
     * @param startDate 开始日期，格式：yyyy-MM-dd
     * @param endDate 结束日期，格式：yyyy-MM-dd
     * @return 价值趋势列表
     */
    @GetMapping("/value-trend")
    @ApiOperation("获取资产价值趋势")
    public Result<List<Object>> getAssetValueTrend(
            @RequestParam String startDate, 
            @RequestParam String endDate) {
        List<Object> trend = statisticsService.getAssetValueTrend(startDate, endDate);
        return Result.success(trend);
    }

    /**
     * 获取资产台账列表
     * @return 资产台账列表
     */
    @GetMapping("/asset-ledger")
    @ApiOperation("获取资产台账")
    public Result<List<AssetInfo>> getAssetLedger() {
        List<AssetInfo> ledgerList = statisticsService.getAssetLedgerList();
        return Result.success(ledgerList);
    }

    /**
     * 获取资产流转报表数据
     * @return 流转报表数据
     */
    @GetMapping("/transfer-report")
    @ApiOperation("获取资产流转报表")
    public Result<List<Map<String, Object>>> getAssetTransferReport() {
        List<Map<String, Object>> report = statisticsService.getAssetTransferReport();
        return Result.success(report);
    }

    /**
     * 获取部门领用热度数据
     * @return 部门领用热度数据
     */
    @GetMapping("/dept-usage-hotness")
    @ApiOperation("获取部门领用热度")
    public Result<List<Map<String, Object>>> getDeptUsageHotness() {
        List<Map<String, Object>> hotness = statisticsService.getDeptUsageHotness();
        return Result.success(hotness);
    }
    
    /**
     * 获取盘点记录报表
     * @param params 查询参数
     * @return 盘点记录报表数据
     */
    @GetMapping("/asset/check")
    @ApiOperation("获取盘点记录报表")
    public Result<List<Map<String, Object>>> getCheckRecordReport(@RequestParam Map<String, Object> params) {
        try {
            List<Map<String, Object>> report = assetCheckService.getCheckRecordReport(params);
            return Result.success(report);
        } catch (Exception e) {
            log.error("获取盘点记录报表失败", e);
            return Result.error("获取盘点记录报表失败");
        }
    }
    
    /**
     * 获取盘点统计分析数据
     * @param planId 盘点计划ID
     * @return 盘点统计数据
     */
    @GetMapping("/check/statistics")
    @ApiOperation("获取盘点统计分析")
    public Result<Map<String, Object>> getCheckStatistics(@RequestParam Long planId) {
        Map<String, Object> statistics = assetCheckService.getCheckStatistics(planId);
        return Result.success(statistics);
    }
    
    /**
     * 获取部门盘点情况统计
     * @return 部门盘点统计数据
     */
    @GetMapping("/check/dept-statistics")
    @ApiOperation("获取部门盘点情况统计")
    public Result<List<Map<String, Object>>> getDeptCheckStatistics() {
        try {
            List<Map<String, Object>> statistics = assetCheckService.getDeptCheckStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取部门盘点统计失败", e);
            return Result.error("获取部门盘点统计失败");
        }
    }
    
    /**
     * 获取盘点完成率趋势
     * @param startDate 开始日期，格式：yyyy-MM-dd
     * @param endDate 结束日期，格式：yyyy-MM-dd
     * @return 盘点完成率趋势数据
     */
    @GetMapping("/check/trend")
    @ApiOperation("获取盘点完成率趋势")
    public Result<List<Map<String, Object>>> getCheckCompletionRateTrend(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        try {
            List<Map<String, Object>> trend = assetCheckService.getCheckCompletionRateTrend(startDate, endDate);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取盘点完成率趋势失败", e);
            return Result.error("获取盘点完成率趋势失败");
        }
    }
}